// pages/mockexam/special/single/detail/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    list: [],
    questionList: [],
    optionList: [],
    listIndex: 0,
    listSize: 0,
    questionIndex: 0,
    qmaxSize: 0,
    qcount: 0,
    submitObj: {},
    flag: true,
    resultInfo: {},
    serverUrl: app.data.server,
    showButton: false,
    sure: false,
    showImage: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    app.paiRequest("/app/mockexam/special/picture/findList", { parentCode: options.parentCode }).then(function (res) {
      var dataList = res.data;
      _this.setData({
        list: dataList,
        listSize: dataList.length,
        options: options
      })
      var oneObj = dataList[0];
      app.paiRequest("/app/mockexam/special/picture/listQuestion", { parentCode: oneObj.id }).then(function (ress) {

        var childList = ress.data;
        _this.setData({
          questionList: childList,
          qmaxSize: childList.length,
          qcount: childList.length
        })      
        
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  nextquestion: function () {
    this.setData({
      questionIndex: this.data.questionIndex + 1
    })
  },
  nextList: function () {
    var _this = this;
    var questionList = this.data.questionList;
    var questionIndex = this.data.questionIndex;
    var obje = questionList[questionIndex + 1];
    if (obje == undefined) {
      var subjectList = this.data.list;
      var subjectIndex = this.data.listIndex;
      if (subjectList[subjectIndex + 1] == undefined) {
        this.setData({
          showButton: true,
          sure: false,
          showImage: false
        })
      } else {
        app.paiRequest("/app/mockexam/special/picture/listQuestion", {
          parentCode: subjectList[subjectIndex + 1].id
        }).then(function (res) {
          var questionList = res.data;         
            _this.setData({
              questionList: questionList,
              questionIndex: 0,
              listIndex: subjectIndex + 1,
              sure: false,
              showImage: false
            })
        })
      }
    } else {      
        _this.setData({
          questionIndex: questionIndex + 1,
          sure: false,
          showImage: false
        })
      
    }
  },
  radioChang: function (e) {
    var questionId = e.currentTarget.dataset.id;
    var objtype = e.currentTarget.dataset.objtype;
    var answerId = e.detail.value;
    var options = this.data.options;
    var postid = e.currentTarget.dataset.postid;
    var _this = this;
    app.paiRequest("/app/mockexam/special/picture/saveRecord", {
      specialId: options.parentCode,
      type: 'picture',
      answerId: answerId,
      postId: postid,
      questionId: questionId,
    }).then(function (res) {
      console.log(res)
      var data = res.data;
      var score = data.score;
      if (score == undefined) {

      } else {
        if (score > 0) {
          _this.setData({
            sure: true,
            showImage: true
          })
        } else {
          _this.setData({
            sure: false,
            showImage: true
          })
        }
      }
    })
  },
  checkChang: function (e) {
    var questionId = e.currentTarget.dataset.id;
    var objtype = e.currentTarget.dataset.objtype;
    var answerId = e.detail.value;
    var options = this.data.options;
    app.paiRequest("/app/mockexam/special/picture/saveRecord", {
      specialId: options.parentCode,
      type: 'picture',
      answerId: answerId,
      questionId: questionId,
    }).then(function (res) {
      console.log(res)
    })
  },
  submitQuestion: function () {
    var _this = this;
    var options = this.data.options;
    app.paiRequest("/app/mockexam/special/user/compute", {
      specialId: options.parentCode,
      type: 'picture',
      userSpecialId: options.pictureId
    }).then(function (res) {
      var data = res.data;
      _this.setData({
        resultInfo: data,
        flag: false
      })
    })
  },
  // 遮罩层隐藏
  conceal: function () {
    this.setData({ flag: true })
    wx.navigateBack({
      delta: 1
    })
  },

})